package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.models.bo.SysPermissionBO;
import com.yt.sys.models.bo.SysPermissionSaveBO;
import com.yt.sys.models.dto.SysPermissionDTO;
import com.yt.sys.models.dto.SysRolePermissionDTO;
import com.yt.sys.models.service.ISysPermissionService;
import com.yt.sys.models.service.ISysRolePermissionService;
import com.yt.sys.models.vo.SysPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统权限信息控制器
 *
 * @author zhou
 * @Date 2025-09-03 15:12:37
 */

@RestController
@RequestMapping("/api/sysPermission")
@Slf4j
@Api(value = "SysPermissionApiController", tags = "系统权限信息Api接口")
public class SysPermissionApiController {

    @DubboReference
    private ISysPermissionService sysPermissionService;
    @DubboReference
    private ISysRolePermissionService sysRolePermissionService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SysPermissionDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        SysPermissionDTO dto = sysPermissionService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "系统权限信息列表", notes = "系统权限信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysPermissionDTO>> queryPageList(SysPermissionBO bo) {
        Wrapper<SysPermissionDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysPermissionDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysPermissionService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/addSysPermission")
    @ApiOperation(value = "增加权限信息", notes = "增加权限信息")
    public Result<Boolean> addSysPermission(@RequestBody SysPermissionSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = sysPermissionService.add(saveBO);

        return new Result<>(flag);
    }

    @PostMapping("/deleteSysPermission")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息")
    @Transactional
    public Result<Boolean> deleteSysPermission(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //1.删除权限表
        boolean flag = sysPermissionService.deleteById(id);
        //2.删除角色权限表
        Wrapper<SysRolePermissionDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("permission_id", id);
        sysRolePermissionService.delete(wrapper);
        return new Result<>(flag);
    }

    @PostMapping("/querySysPermission")
    @ApiOperation(value = "查询权限信息", notes = "查询权限信息")
    public Result<List<SysPermissionVO>> querySysPermission() {
        List<SysPermissionVO> sysPermissionVOS = sysPermissionService.queryPermission();
        return new Result<>(sysPermissionVOS);
    }

    @PostMapping("/updateSysPermission")
    @ApiOperation(value = "修改权限信息", notes = "修改权限信息")
    public Result<Boolean> updateSysPermission(@RequestBody SysPermissionSaveBO saveBO) {
        SysPermissionDTO sysPermissionDTO = new SysPermissionDTO();
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        BeanUtils.copyProperties(saveBO, sysPermissionDTO);
        boolean flag = sysPermissionService.updateById(sysPermissionDTO);

        return new Result<>(flag);
    }
}
